package cn.com.jrr.core.service;

import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.com.jrr.core.common.ToolUtils;
import cn.com.jrr.core.dao.impl.BaseDaoImpl;
import cn.com.jrr.core.model.DataTableRequest;
import cn.com.jrr.core.model.DataTableResponse;
import cn.com.jrr.core.model.PageModel;
import cn.com.jrr.core.model.Student;
@Service
public class StudentService {
	@Resource
	private BaseDaoImpl<Student> baseDaoImpl;
	@Autowired
	PageModel <Student> pageModel;

	/**
	 * 查询数据
	 * */
	public DataTableResponse<Student> getAllUser(DataTableRequest dataTableRequest){
		pageModel.setCurrentItem(dataTableRequest.getCurrentItem());
		pageModel.setPageSize(dataTableRequest.getPageSize());
		String sql="select * from student order by id desc";
		Object [] obj=new String[]{"id","name","code","sex","className","school"};
		sql=ToolUtils.SearchAndSortHql(sql, dataTableRequest, obj);
		DataTableResponse<Student> dataTableResponse=baseDaoImpl.findBySql(sql,pageModel,Student.class);
		return  dataTableResponse;
	}
	/**
	 * 添加数据
	 * */
	public int add(Student student){
		return baseDaoImpl.updateBySql("insert into student(name,code,sex,className,school) values(?,?,?,?,?)",
				new Object[]{
						student.getName(),
						student.getCode(),
						student.getSex(),
						student.getClassName(),
						student.getSchool()
				});
	}
	/**
	 * 获取一条
	 * */
	public Student get(Integer id){
		return  baseDaoImpl.get(Student.class, id);
	}
	/**
	 * 删除记录
	 * */
	public int delete(Integer id){
		return  baseDaoImpl.updateBySql("delete from student where id=?", new Object[]{id});
	}
	/**
	 * 更新记录
	 * */
	public int update(Student student){
		return  baseDaoImpl.updateBySql("update student set name=?,code=?,sex=?,className=?,school=? where id=?", new Object[]{
				student.getName(),
				student.getCode(),
				student.getSex(),
				student.getClassName(),
				student.getSchool(),
				student.getId()});
	}

}
